package kernel.udp.requests;


// - Parse the request string into a request object
public class Request {

	// - e.g. RequestManager.register<|>Type1-Value1<|>Type2-Value2<|>Type3-Value3
	public String ModuleName;	
	public String ServiceName;
	public long reqNo;
	
	public Object[] Parameters;

	// - Create a request object from socket string.
	public Request(String requestString){
		
		String request = requestString.trim();
	
		reqNo = new Long(request.substring(0,request.indexOf("<|>"))).longValue();
		request = request.substring(request.indexOf("<|>")+3);
		
			
		ModuleName = request.substring(0, request.indexOf("."));
		request = request.substring(request.indexOf(".")+1);
		ServiceName = request.substring(0, request.indexOf("<|>"));
					
		Parameters = new Object[countParams(request)];
		int cnt=0;
		while (request.indexOf("<|>") >= 0){

			request = request.substring(request.indexOf("<|>")+3);
			String Type = request.substring(0, request.indexOf("-"));
			request = request.substring(request.indexOf("-")+1);
			
			String Value;
			if (request.indexOf("<|>") >= 0)
				Value = request.substring(0, request.indexOf("<|>"));			
			else
				Value = request.substring(0);

			
			if (Type.equals("String")) Parameters[cnt] = new String(Value);
			else if (Type.equals("Double")) Parameters[cnt] = new Double(Value);
			else if (Type.equals("Integer")) Parameters[cnt] = new Integer(Value);
			else if (Type.equals("Boolean")) Parameters[cnt] = new Boolean(Value);		                                      
			                                      
			cnt++;
		}
		
	}
	
	public int countParams(String request){
		
		int cnt=0;
		String req = request;
		while (req.indexOf("<|>")>=0){
		  req = req.substring(req.indexOf("<|>")+3);
		  cnt++;
		}
		
		return cnt;
	}
	
	// - Create a null request object.
	public Request(String ModServiceName, Object[] Param){		
		ModuleName = ModServiceName.substring(0, ModServiceName.indexOf("."));
		ServiceName = ModServiceName.substring(ModServiceName.indexOf(".")+1);		
		Parameters = Param;		
	}
	
	
	public Class[] ParamTypes(){

		Class[] Types = new Class[Parameters.length];
		
		for (int i=0; i<Parameters.length; i++){
			
			if (Parameters[i] instanceof String)
				Types[i] = String.class;
			else if (Parameters[i] instanceof Integer)
				Types[i] = int.class;
			else if (Parameters[i] instanceof Double)
				Types[i] = double.class;
			else if (Parameters[i] instanceof Boolean)
				Types[i] = boolean.class;
			
			//System.out.println("[ "+"UDP CLIENT"+" ] "+"[ Incoming ] [ Parameter["+(i+1)+"] ] [ Type = "+Types[i].getName()+" ] [ Value = "+Parameters[i].toString()+" ]");
		}
		
		return Types;
	}
	
	// - Convert Request to an UDP Content to be sent across. 
	public String toUDPString(){
		
		String result="";		
		result += ModuleName+"."+ServiceName;
		
		for (int i=0; i<Parameters.length; i++){
			
			if (Parameters[i] instanceof String)
				result += "<|>" + "String"+"-"+(String)Parameters[i];
			else if (Parameters[i] instanceof Integer)
				result += "<|>" + "Integer"+"-"+((Integer)Parameters[i]).toString();
			else if (Parameters[i] instanceof Double)
				result += "<|>" + "Double"+"-"+((Double)Parameters[i]).toString();
			else if (Parameters[i] instanceof Boolean)
				result += "<|>" + "Boolean"+"-"+((Boolean)Parameters[i]).toString();			
		}
				
		return result;
	}
	
	public String toParamString(){
		
		String result="";
	
		for (int i=0; i<Parameters.length; i++){
			if (Parameters[i] instanceof String)
				result += "String-"+""+(String)Parameters[i]+", ";
			else if (Parameters[i] instanceof Integer)
				result += "Integer"+"-"+((Integer)Parameters[i]).toString()+", ";
			else if (Parameters[i] instanceof Double)
				result += "Double"+"-"+((Double)Parameters[i]).toString()+", ";
			else if (Parameters[i] instanceof Boolean)
				result += "Boolean-"+((Boolean)Parameters[i]).toString()+", ";			
		
		}
		if (result.length()>2){
			result = result.substring(0, result.length()-2);
		}
		return result;
	}
	
	public String toString(){
		
		String res = "";
		
		res+="" + ModuleName+"."+ServiceName;
		
		return res;
	}
	
}
